//
//  WaterFallViewController.m
//  DebugDemo
//
//  Created by XFB on 2019/6/29.
//  Copyright © 2019 ZhouCheng. All rights reserved.
//  瀑布流

#import "WaterFallViewController.h"
#import "WaterFlowLayout.h"
#import "WaterFlowImageModel.h"
#import "WaterFlowCollectionViewCell.h"

@interface WaterFallViewController () <UICollectionViewDataSource, UICollectionViewDelegate, WaterFlowLayoutDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) WaterFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation WaterFallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.collectionView];
}

#pragma mark --- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WaterFlowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WaterFlowCollectionViewCell" forIndexPath:indexPath];
    WaterFlowImageModel *model = self.dataSource[indexPath.item];
    cell.model = model;
//    cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    return cell;
}

#pragma mark --- WaterFlowLayoutDelegate

- (CGFloat)waterFlowLayout:(WaterFlowLayout *_Nullable)waterFlowLayout
                 itemWidth:(CGFloat)itemWidth
                 indexPath:(NSIndexPath *_Nonnull)indexPath
{
    WaterFlowImageModel *model = self.dataSource[indexPath.item];
    CGFloat width = model.imageWidth;
    CGFloat height = model.imageHeight;
    return height / width * itemWidth;
}

#pragma mark --- Lazy loading
- (WaterFlowLayout *)flowLayout
{
    if (!_flowLayout) {
        _flowLayout = [[WaterFlowLayout alloc] initWithColumnCount:3];
        _flowLayout.delegate = self;
        [_flowLayout setColumnSpacing:5 rowSpacing:5 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
    }
    return _flowLayout;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-NavBarHeight-BottomHeight) collectionViewLayout:self.flowLayout];
//        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"WaterFlowCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WaterFlowCollectionViewCell"];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"WaterFlowImageModel.plist" ofType:nil];
        NSArray *plistArray = [NSArray arrayWithContentsOfFile:plistPath];
        NSArray *modelArray = [WaterFlowImageModel mj_objectArrayWithKeyValuesArray:plistArray];
        _dataSource = [modelArray mutableCopy];
    }
    return _dataSource;
}

@end
